<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

class TaxRatesComponent
{
	function TaxRatesComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else
			return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$paginate = (boolean)$params['paginate'];
		$max_rows = (int)$params['max_rows'];
		$offset = (int)$params['offset'];

		$sqlQuery = 'select tr.tax_rates_id, tr.tax_zone_id, tr.tax_class_id, tr.tax_priority, tr.tax_rate, tr.tax_description ' .
		'from ' . TABLE_TAX_RATES . ' tr';
		if ($paginate) {
			$sqlQuery .= ' limit ' . $offset . ', ' . $max_rows;
		}
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle}{$i}{'id'}= (int)$row['tax_rates_id'];
			$value{$handle}{$i}{'tax_zone_id'}= (int)$row['tax_zone_id'];
			$value{$handle}{$i}{'tax_class_id'}= (int)$row['tax_class_id'];
			$value{$handle}{$i}{'priority'}= (int)$row['tax_priority'];
			$value{$handle}{$i}{'rate'}= (double)$row['tax_rate'];
			$value{$handle}{$i}{'description'}= $row['tax_description'];
			$i++;
		}

		if ($paginate) {
			$sqlQuery = 'select count(tax_rates_id) as cnt from ' . TABLE_TAX_RATES;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$cnt = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$cnt = (int)$row['cnt'];
			}

			$value{$handle . 'MetaData'}{'max_rows'}= (int)$max_rows;
			$value{$handle . 'MetaData'}{'offset'}= $offset;
			$value{$handle . 'MetaData'}{'page_links_max'} = (int)MAX_DISPLAY_PAGE_LINKS;
			$value{$handle . 'MetaData'}{'rows'}= $i;
			$value{$handle . 'MetaData'}{'total_rows'}= $cnt;
		}

		return $value;
	}

	function edit(&$params) 
	{
		$i = 0;
		while (true)
		{
			$i++;
			if ($i > 100) break;

			$taxRateId = (int)$params['taxRateId-'.$i];
			if ($taxRateId == 0) break;

			$taxDescription = prepare_input($params['taxRateDescription'.$i]);
			if ($taxDescription == null || strlen($taxDescription) == 0) continue;

			$taxZoneId = (int)$params['taxRateZoneId-'.$i];
			$taxClassId = (int)$params['taxRateClassId-'.$i];
			$taxPriority = (int)$params['taxRatePriority-'.$i];
			$taxRate = (double)$params['taxRate-'.$i];
			$deleteRow = (boolean)$params['taxRateDelete-'.$i];
					
			if ($deleteRow) {
				if ($taxRateId < 1) continue;
				$sql = 'delete from ' . TABLE_TAX_RATES . ' where tax_rates_id=' . $taxRateId;
				mysql_query($sql) or die(mysql_error());
			}
			else {	
				$sql_data_array = array();
				$sqlAction = 'insert';
				$where = '';
				if ($taxRateId > 0) {
					$sqlAction = 'update';
					$where = 'tax_rates_id=' . $taxRateId;
				}
				else {
					$sql_data_array['date_added'] = 'now()';
				}
				$sql_data_array['tax_zone_id'] = $taxZoneId;
				$sql_data_array['tax_class_id'] = $taxClassId;
				$sql_data_array['tax_priority'] = $taxPriority;
				$sql_data_array['tax_rate'] = $taxRate;
				$sql_data_array['tax_description'] = $taxDescription;
				$sql_data_array['last_modified'] = 'now()';
				db_perform(TABLE_TAX_RATES, $sql_data_array, $sqlAction, $where);
			}
		}

		return $this->query($params);
	}

}
?>
